package com.smxy.hunzi.user.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.smxy.hunzi.common.utils.Constant;
import com.smxy.hunzi.user.openfeign.ArticleFeignService;
import com.smxy.hunzi.user.valid.group.Register;
import com.smxy.hunzi.user.vo.UserLoginVo;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authc.IncorrectCredentialsException;
//import org.apache.shiro.authc.UnknownAccountException;
//import org.apache.shiro.authc.UsernamePasswordToken;
//import org.apache.shiro.subject.Subject;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.smxy.hunzi.user.entity.UserEntity;
import com.smxy.hunzi.user.service.UserService;
import com.smxy.hunzi.common.utils.PageUtils;
import com.smxy.hunzi.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 用户信息表
 *
 * @author litao
 * @email 4028546@qq.com
 * @date 2020-08-14 12:35:17
 */
@RestController
@RequestMapping("/user/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private ArticleFeignService articleFeignService;

    /**
     * 列表
     */
    @RequestMapping("/list")
   // @RequiresPermissions("article:user:list")
    public R list(@RequestParam Map<String, Object> params, HttpServletRequest httpServletRequest){
//        String token = httpServletRequest.getHeader("isadmin");
//        System.out.println("-----"+token);

        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
   // @RequiresPermissions("article:user:info")
    public R info(@PathVariable("id") Long id){
		UserEntity user = userService.getById(id);

        return R.ok().put("user", user).put("nickname", user.getNickname());
    }

    /**
     * 保存
     */
    @RequestMapping("/register")
   // @RequiresPermissions("article:user:save")
    public R save(@Validated({Register.class}) @RequestBody UserEntity user){
		return userService.register(user);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
  //  @RequiresPermissions("article:user:update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);
        return R.ok();
    }
    @RequestMapping("/updatepassword")
    public R updatePassword(@RequestBody UserEntity userEntity){
        return userService.updatePassword(userEntity);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("article:user:delete")
    public R delete(@RequestBody Long[] ids){
		userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/login")
    public R login(@RequestBody UserLoginVo userLoginVo){
        return userService.login(userLoginVo);
    }
    @RequestMapping("/updateuserstatus")
    public R userStatus(@RequestBody UserEntity userEntity, HttpServletRequest httpServletRequest) throws Exception {
        String isAdmin = httpServletRequest.getHeader("isadmin");
        String token = httpServletRequest.getHeader("token");
        return userService.updateStatus(userEntity,isAdmin,token);
    }
    @RequestMapping("/updateperms")
    public R updatePerms(@RequestBody UserEntity userEntity,HttpServletRequest request){
        String isAdmin = request.getHeader("isadmin");
        if(!"yes".equals(isAdmin)){
            return R.error(1405, "你不是系统管理员，无法进行该操作");
        }
        return userService.updatePerms(userEntity);
    }

}
